var t = require("../../api.js"), a = getApp(), i = !1, e = !1;
var u = require("../../api.js"),
  t = u,
  h = u,
  rr = require("../../utils.js"),
  j = a,
  o = a;
Page({
    data: {
        cat_id: "",
        page: 1,
        cat_list: [],
        goods_list: [],
        sort: 0,
        sort_type: -1,
        quick_icon: !0,
        cat_info:[],
        is_group: 0
    },
    onLoad: function(t) {
      if (t.is_group == 1) {
        this.setData({
          is_group: t.is_group
        })
      }
      a.pageOnLoad(this, t), this.loadData(t);
    },
    loadData: function(t) {
        var a = this, i = wx.getStorageSync("cat_list"), e = "";
        if (t.cat_id) for (var s in i) {
            var o = !1;
            i[s].id == t.cat_id && (i[s].checked = !0, i[s].list.length > 0 && (e = "height-bar"));
            for (var d in i[s].list) i[s].list[d].id == t.cat_id && (i[s].list[d].checked = !0, 
            o = !0, e = "height-bar");
            o && (i[s].checked = !0);
        }
        if (t.cat_id){
          a.getCatInfo(t.cat_id);
          this.initTabObserver()
        }
        if (t.goods_id) var r = t.goods_id;
        a.setData({
            cat_list: i,
            cat_id: t.cat_id || "",
            height_bar: e,
            goods_id: r || ""
        }), a.reloadGoodsList();
      this.themeColor()
    },
    getCatInfo: function (o) {
      var that = this;
      a.request({
        url: t.default.cat_info,
        data: {
          cat_id: o
        },
        success: function (t) {
          that.setData({
            cat_info : t
          })
        },
        complete: function () {
          
        }
      });
    },
    themeColor: function () {
      var cl = wx.getStorageSync('_navigation_bar_color').themeColor;
      var cl_b = !cl || cl == '#ffffff' ? 'ff4544' : cl;
      var color_rgb = this.hexToRgb(cl_b);
      var back_g = 'rgba(' + color_rgb.r + ',' + color_rgb.g + ',' + color_rgb.b + ',.1)'
      this.setData({
        all_color: cl ? cl : '#ffffff',
        back_g: back_g
        // back_end: back_end
      });
    },
    hexToRgb: function (hex) {
      var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
      return result ? {
        r: parseInt(result[1], 16),
        g: parseInt(result[2], 16),
        b: parseInt(result[3], 16)
      } : null;
    },
    catClick: function(t) {
        var a = this, i = "", e = t.currentTarget.dataset.index, s = a.data.cat_list;
        for (var o in s) {
            for (var d in s[o].list) s[o].list[d].checked = !1;
            o == e ? (s[o].checked = !0, i = s[o].id) : s[o].checked = !1;
        }
        var r = "";
        s[e].list.length > 0 && (r = "height-bar"), a.setData({
            cat_list: s,
            cat_id: i,
            height_bar: r
        }), a.reloadGoodsList();
    },
    quickNavigation: function() {
        this.setData({
            quick_icon: !this.data.quick_icon
        });
        this.data.store;
        var t = wx.createAnimation({
            duration: 300,
            timingFunction: "ease-out"
        });
        this.data.quick_icon ? t.opacity(0).step() : t.translateY(-55).opacity(1).step(), 
        this.setData({
            animationPlus: t.export()
        });
    },
    subCatClick: function(t) {
        var a = this, i = "", e = t.currentTarget.dataset.index, s = t.currentTarget.dataset.parentIndex, o = a.data.cat_list;
        for (var d in o) for (var r in o[d].list) d == s && r == e ? (o[d].list[r].checked = !0, 
        i = o[d].list[r].id) : o[d].list[r].checked = !1;
        a.setData({
            cat_list: o,
            cat_id: i
        }), a.reloadGoodsList();
    },
    allClick: function() {
        var t = this, a = t.data.cat_list;
        for (var i in a) {
            for (var e in a[i].list) a[i].list[e].checked = !1;
            a[i].checked = !1;
        }
        t.setData({
            cat_list: a,
            cat_id: "",
            height_bar: ""
        }), t.reloadGoodsList();
    },
    reloadGoodsList: function() {
        var i = this;
        e = !1, i.setData({
            page: 1,
            goods_list: [],
            show_no_data_tip: !1
        });
        var s = i.data.cat_id || "", o = i.data.page || 1;
        a.request({
            url: t.default.goods_list,
            data: {
                cat_id: s,
                page: o,
                sort: i.data.sort,
                sort_type: i.data.sort_type,
                goods_id: i.data.goods_id,
                shop_id: wx.getStorageSync('address_info').shop_id,
                is_group: i.data.is_group
            },
            success: function(t) {
                0 == t.code && (0 == t.data.list.length && (e = !0), i.setData({
                    page: o + 1
                }), i.setData({
                    goods_list: t.data.list,
                })), i.setData({
                    show_no_data_tip: 0 == i.data.goods_list.length
                });
                if(t.data.cat_name){
                  wx.setNavigationBarTitle({
                    title: t.data.cat_name
                  })
                }
            },
            complete: function() {}
        });
    },
    loadMoreGoodsList: function() {
        var s = this;
        if (!i) {
            s.setData({
                show_loading_bar: !0
            }), i = !0;
            var o = s.data.cat_id || "", d = s.data.page || 2, r = s.data.goods_id;
            a.request({
                url: t.default.goods_list,
                data: {
                    page: d,
                    cat_id: o,
                    sort: s.data.sort,
                    sort_type: s.data.sort_type,
                    goods_id: r,
                    shop_id: wx.getStorageSync('address_info').shop_id,
                    is_group: s.data.is_group
                },
                success: function(t) {
                    0 == t.data.list.length && (e = !0);
                    var a = s.data.goods_list.concat(t.data.list);
                    s.setData({
                        goods_list: a,
                        page: d + 1
                    });
                },
                complete: function() {
                    i = !1, s.setData({
                        show_loading_bar: !1
                    });
                }
            });
        }
    },
    onReachBottom: function() {
        var t = this;
        e || t.loadMoreGoodsList();
    },
    onShow: function(t) {
        a.pageOnShow(this);
        var i = this;
        if (wx.getStorageSync("list_page_reload")) {
            var e = wx.getStorageSync("list_page_options");
            wx.removeStorageSync("list_page_options"), wx.removeStorageSync("list_page_reload");
            var s = e.cat_id || "";
            i.setData({
                cat_id: s
            });
            var o = i.data.cat_list;
            for (var d in o) {
                var r = !1;
                for (var c in o[d].list) o[d].list[c].id == s ? (o[d].list[c].checked = !0, r = !0) : o[d].list[c].checked = !1;
                r || s == o[d].id ? (o[d].checked = !0, o[d].list && o[d].list.length > 0 && i.setData({
                    height_bar: "height-bar"
                })) : o[d].checked = !1;
            }
            i.setData({
                cat_list: o
            }), i.reloadGoodsList();
        }
    },
    sortClick: function(t) {
        var a = this, i = t.currentTarget.dataset.sort, e = void 0 == t.currentTarget.dataset.default_sort_type ? -1 : t.currentTarget.dataset.default_sort_type, s = a.data.sort_type;
        if (a.data.sort == i) {
            if (-1 == e) return;
            s = -1 == a.data.sort_type ? e : 0 == s ? 1 : 0;
        } else s = e;
        a.setData({
            sort: i,
            sort_type: s
        }), a.reloadGoodsList();
    },
    onReady: function() {},
    onHide: function() {},
    onUnload: function() {},
    onShareAppMessage: function(t) {
        return {
            path: "/pages/list/list?user_id=" + wx.getStorageSync("user_info").id,
            success: function(t) {}
        };
    },
    onPullDownRefresh: function() {},
  /**
 * 初始化观察器
 */
  initTabObserver() { return false;
    this.tabObserver = wx.createIntersectionObserver(this)
    this.tabObserver
      // 相对于页面可视区
      .relativeToViewport()
      // 相对于某一个元素
      // .relativeTo('.fixed-position')
      .observe('.image_box', (res) => {
        const visible = res.intersectionRatio > 0
        this.setData({ tabFixed: !visible })
      })
  },
  onPageScroll: function () {
    this.initTabObserver();
  },
 


  hideAttrPicker: function () {
    this.setData({
      show_attr_picker: false
    });
  },

  numberSub: function () {
    var goods_num = this.data.goods_num;
    if (goods_num > 1) {
      goods_num--;
    }
    var minusStatus = goods_num <= 1 ? 'disabled' : 'normal';
    this.setData({
      goods_num: goods_num,
      minusStatus: minusStatus
    });
  },
  numberAdd: function () {
    var goods_num = this.data.goods_num;
    goods_num++;
    var minusStatus = goods_num < 1 ? 'disabled' : 'normal';
    this.setData({
      goods_num: goods_num,
      minusStatus: minusStatus
    });
  },


  attrGoodsClick: function (a) {
    var i = this,
      r = a.target.dataset.groupId,
      e = a.target.dataset.id,
      s = i.data.attr_group_list;
    for (var d in s)
      if (s[d].attr_group_id == r)
        for (var n in s[d].attr_list) s[d].attr_list[n].attr_id == e ? s[d].attr_list[n].checked = !0 : s[d].attr_list[n].checked = !1;
    i.setData({
      attr_group_list: s
    });
    var c = [],
      u = !0;
    for (var d in s) {
      var _ = !1;
      for (var n in s[d].attr_list)
        if (s[d].attr_list[n].checked) {
          c.push(s[d].attr_list[n].attr_id), _ = !0;
          break;
        }
      if (!_) {
        u = !1;
        break;
      }
    }
    u && (wx.showLoading({
      title: "正在加载",
      mask: !0
    }), j.request({
      url: t.default.goods_attr_info,
      data: {
        goods_id: i.data.goods_info.id,
        attr_list: JSON.stringify(c)
      },
      success: function (t) {
        if (wx.hideLoading(), 0 == t.code) {
          var a = i.data.goods_info;
          a.price = t.data.price, a.num = t.data.num, a.attr_pic = t.data.pic, i.setData({
            goods_info: a
            // goods :a
          });
        }
      }
    }));
  },
  // 加入购物车
  addToCart: function () {
    this.submit('ADD_CART')
  },


  submit: function (a) {
    var i = this;
    // if (!i.data.show_attr_picker) return i.setData({
    //     show_attr_picker: !0
    // }), !0;
    if (i.data.miaosha_data && i.data.miaosha_data.rest_num > 0 && i.data.form.number > i.data.miaosha_data.rest_num) return wx.showToast({
      title: "商品库存不足，请选择其它规格或数量",
      image: "/images/icon-warning.png"
    }), !0;
    if (i.data.goods_num > i.data.goods_info.num) return wx.showToast({
      title: "商品库存不足，请选择其它规格或数量",
      image: "/images/icon-warning.png"
    }), !0;
    var r = i.data.attr_group_list,
      e = [];
    for (var s in r) {
      var d = !1;
      for (var n in r[s].attr_list)
        if (r[s].attr_list[n].checked) {
          d = {
            attr_id: r[s].attr_list[n].attr_id,
            attr_name: r[s].attr_list[n].attr_name
          };
          break;
        }
      if (!d) return wx.showToast({
        title: "请选择" + r[s].attr_group_name,
        image: "/images/icon-warning.png"
      }), !0;
      e.push({
        attr_group_id: r[s].attr_group_id,
        attr_group_name: r[s].attr_group_name,
        attr_id: d.attr_id,
        attr_name: d.attr_name
      });
    }
    if ("ADD_CART" == a && (wx.showLoading({
      title: "正在提交",
      mask: !0
    }), j.request({
      url: t.cart.add_cart,
      method: "POST",
      data: {
        goods_id: i.data.goods_info.id,
        attr: JSON.stringify(e),
        num: i.data.goods_num
      },
      success: function (t) {
        wx.hideLoading(), wx.showToast({
          title: t.msg,
          duration: 1500
        }), i.setData({
          show_attr_picker: !1
        });
        if (!(i.data.goods_cart_index === false)) {
        }
      },
      fail: function (res) {
        wx.hideLoading()
      },
      complete: function (res) {
        wx.hideLoading()
      }

    })), "BUY_NOW" == a) {
      i.setData({
        show_attr_picker: !1
      });
      var c = [];
      c.push({
        goods_id: i.data.id,
        num: i.data.form.number,
        attr: e
      });
      var u = i.data.goods,
        _ = 0;
      null != u.mch && (_ = u.mch.id);
      var g = [];
      g.push({
        mch_id: _,
        goods_list: c
      }), wx.redirectTo({
        url: "/pages/new-order-submit/new-order-submit?mch_list=" + JSON.stringify(g)
      });
    }
  },



});